package com.hc.daoImpl;

import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hc.dao.RunSqlDao;
import com.hc.util.BaseSql;
/**
 * 执行sql语句类
 * @author tiansl
 *
 */
public class RunSqlDaoImpl extends BaseSql implements RunSqlDao{

	@Override
	public boolean runSqlUpdate(String sql) {
		
		openConnect();
		try {
			  sta.executeUpdate(sql);
			  sta.close();
			  con.close();
		} catch (SQLException e) {
			return false;
		}  
		
		return true;
	}

	@Override
	public List runSqlQuery(String sql) {
		
		openConnect();
		List list =new ArrayList();
		 java.sql.ResultSet rs= null;
		try {
		
		
			 	rs = sta.executeQuery(sql);
			  
					ResultSetMetaData data = rs.getMetaData();
				int columnCount = data.getColumnCount();
				while(rs.next()){
					Map map = new HashMap();
					
					for(int i =1 ; i <=columnCount;i++ ){
						
						map.put(data.getColumnName(i), rs.getObject(i));
					}
					list.add(map);
				}
			
			 
			  
			  
			  
		} catch (SQLException e) {
			return list;
		} finally{
			
			 try {
				 rs.close();
				sta.close();
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
	
		} 
		
		
		
		return list;
	}

	
public static void main(String[] args) {
	RunSqlDaoImpl dao = new RunSqlDaoImpl();


String sql = "INSERT INTO npingResult1 (shake,timedelay,urlIndex,cmdTime,ip,userNumber,vlanId1,vlanId2) VALUES ( '12', '2', '234', '2014-09-09 17:28:34', '192.168.0.124', 'root', '127.0.0.1', '127.0.0.1')";
	
for(int i =0 ; i < 1000;i++){
	
	dao.runSqlUpdate(sql);
}


}

@Override
public List runSqlQueryNoKey(String sql) {
	openConnect();
	List list =new ArrayList();
	 java.sql.ResultSet rs= null;
	try {
	
	
		 	rs = sta.executeQuery(sql);
		  
				ResultSetMetaData data = rs.getMetaData();
			int columnCount = data.getColumnCount();
			while(rs.next()){
				List  columnData = new ArrayList();
				
				for(int i =1 ; i <=columnCount;i++ ){
					columnData.add(rs.getObject(i));
//					map.put(data.getColumnName(i), rs.getObject(i));
				}
				list.add(columnData);
			}
		
		 
		  
		  
		  
	} catch (SQLException e) {
		return list;
	} finally{
		
		 try {
			 rs.close();
			sta.close();
			con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}

	} 
	
	
	
	return list;
}
	
}
